package com.jy.appmarket.receiver;

import com.jy.appmarket.download.DownloadService;
import com.jy.appmarket.service.NotificationService;
import com.jy.appmarket.service.PollService;
import com.jy.appmarket.service.UpdateService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetStateReceiver extends BroadcastReceiver {
	private static final String TAG = NetStateReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		NotificationService.startNotificationService(context, 10);
		if (intent == null || intent.getAction() == null
				|| !intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			return;
		}

		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNI = cm.getActiveNetworkInfo();
		if (activeNI == null) {
			return;
		}

		context.startService(new Intent(context, PollService.class));
		UpdateService.startUpdateService(context);
		if (activeNI.isConnected() && activeNI.getType() == ConnectivityManager.TYPE_WIFI) {
			Log.i(TAG, " net is connected ,wifi connect...");
			Intent service = new Intent(context, DownloadService.class);
			service.setAction(DownloadService.ACTION_RESUME_DOWNLOAD);
			context.startService(service);
		}
	}
}
